using MicroMsg.Manager;
using MicroMsg.Plugin.FaceBook;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.Util;
using System;
using System.Diagnostics;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.Page
{
	public class InviteFriends : BasePage
	{
		private FBContact currentContact;
		private WebClient m_wcPostMessage;
		internal Grid LayoutRoot;
		internal StackPanel ContentPanel;
		internal Grid QQArea;
		internal Image fbImg;
		internal TextBlock fbName;
		internal TextBlock QQInfoTxt;
		internal Button inviteBtn;
		private bool _contentLoaded;
		public InviteFriends()
		{
			this.InitializeComponent();
		}
		public static void StartInvite(BasePage originPage, FBContact fbContact)
		{
			originPage.GoToPage("/Source/Plugin/Facebook/UI/InviteFriends.xaml", new object[]
			{
				fbContact
			}, false);
		}
		protected override void OnPageIn(object[] object_0)
		{
			if (object_0 == null || object_0.Length <= 0)
			{
				return;
			}
			this.currentContact = (object_0[0] as FBContact);
			this.fbName.set_Text(this.currentContact.strFacebookName);
			BitmapImage cache = FaceBookMgr.getCache(this.currentContact.nID);
			if (cache != null)
			{
				this.fbImg.set_Source(cache);
				return;
			}
			string text = string.Format("http://graph.facebook.com/{0}/picture", this.currentContact.nID);
			WebClient webClient = new WebClient();
			webClient.add_OpenReadCompleted(new OpenReadCompletedEventHandler(this.w_OpenReadCompleted));
			webClient.OpenReadAsync(new Uri(text), this.currentContact.nID);
		}
		private void w_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
		{
			ulong num = (ulong)e.get_UserState();
			if (this.currentContact == null)
			{
				return;
			}
			BitmapImage bitmapImage = null;
			try
			{
				bitmapImage = FaceBookMgr.addCache(num, e.get_Result());
			}
			catch (Exception ex)
			{
				ex.ToString();
			}
			if (num != this.currentContact.nID)
			{
				return;
			}
			if (bitmapImage != null)
			{
				this.fbImg.set_Source(bitmapImage);
				return;
			}
			this.fbImg.set_Source(new ImageSourceConverter().ConvertFromString("/Resource/Img/CommonUse/DefaultHead.png") as ImageSource);
		}
		protected override void OnPageOut()
		{
		}
		public string GetPostParameters(string strAccessToken)
		{
			string result;
			try
			{
				string text = "wechat";
				string text2 = "invite to wechat";
				string text3 = "http://weixin.qq.com/cgi-bin/readtemplate?t=weixin";
				string facebookMsg = strings.FacebookMsg;
				string text4 = "wechat app";
				string text5 = "http://weixin.qq.com/zh_CN/htmledition/images/weixin/ico_weixin0ca6c3.png";
				string text6 = "access_token=" + strAccessToken;
				if (!string.IsNullOrEmpty(text))
				{
					text6 = text6 + "&caption=" + HttpUtility.UrlEncode(text);
				}
				if (!string.IsNullOrEmpty(text2))
				{
					text6 = text6 + "&description=" + HttpUtility.UrlEncode(text2);
				}
				if (!string.IsNullOrEmpty(text3))
				{
					text6 = text6 + "&link=" + HttpUtility.UrlEncode(text3);
				}
				if (!string.IsNullOrEmpty(facebookMsg))
				{
					text6 = text6 + "&message=" + HttpUtility.UrlEncode(facebookMsg);
				}
				if (!string.IsNullOrEmpty(text4))
				{
					text6 = text6 + "&name=" + HttpUtility.UrlEncode(text4);
				}
				if (!string.IsNullOrEmpty(text5))
				{
					text6 = text6 + "&picture=" + HttpUtility.UrlEncode(text5);
				}
				result = text6;
			}
			catch
			{
				result = "";
			}
			return result;
		}
		public void PostToWall(string userId, string AccessToken)
		{
			if (string.IsNullOrEmpty(AccessToken))
			{
				return;
			}
			if (string.IsNullOrEmpty(userId))
			{
				return;
			}
			if (this.m_wcPostMessage == null)
			{
				this.m_wcPostMessage = new WebClient();
				this.m_wcPostMessage.add_UploadStringCompleted(new UploadStringCompletedEventHandler(this.m_wcPostMessage_UploadStringCompleted));
			}
			try
			{
				this.m_wcPostMessage.UploadStringAsync(FBUris.GetPostMessageUri(userId), "POST", this.GetPostParameters(AccessToken));
			}
			catch (Exception ex)
			{
				ex.ToString();
			}
		}
		private void m_wcPostMessage_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
		{
			if (e.get_Error() != null)
			{
				MessageBoxManager.ShowToast(strings.QQFriend_Invite_fail, 2500, null, false);
				return;
			}
			MessageBoxManager.ShowToast(strings.QQFriend_Invite_OK, 2500, null, false);
			ServiceCenter.statReportMgr.addStatItem(enKVStatKey.MM_FACEBOOK_INVITE, "1");
		}
		private void inviteBtn_Click(object sender, RoutedEventArgs e)
		{
			if (this.currentContact == null)
			{
				return;
			}
			this.PostToWall(this.currentContact.nID.ToString(), AccountMgr.getCurAccount().FBToken);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Facebook/UI/InviteFriends.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.ContentPanel = (StackPanel)base.FindName("ContentPanel");
			this.QQArea = (Grid)base.FindName("QQArea");
			this.fbImg = (Image)base.FindName("fbImg");
			this.fbName = (TextBlock)base.FindName("fbName");
			this.QQInfoTxt = (TextBlock)base.FindName("QQInfoTxt");
			this.inviteBtn = (Button)base.FindName("inviteBtn");
		}
	}
}
